package leetcode.D300.T241;

import java.util.*;

class Solution {
    /*// 解法一：分治法
    public List<Integer> diffWaysToCompute(String expression) {
        char[] cArr = expression.toCharArray();
        return dfs(0, cArr.length-1, cArr);
    }
    private List<Integer> dfs(int left, int right, char[] cArr) {
        int idx = left, num = cArr[idx] - '0';
        List<Integer> result = new LinkedList<>();
        while(idx < right && Character.isDigit(cArr[idx+1])) {
            num = num * 10 + (cArr[++idx] - '0');
        }
        if(idx == right) {
            result.add(num);
            return result;
        }
        // merge
        for(int i=idx+1; i<=right; ++i) {
            if(Character.isDigit(cArr[i])) {
                continue;
            }
            List<Integer> leftResult = dfs(left, i-1, cArr);
            List<Integer> rightResult = dfs(i+1, right, cArr);
            char opt = cArr[i];
            for (Integer leftValue : leftResult) {
                for (Integer rightValue : rightResult) {
                    if(opt == '+') {
                        result.add(leftValue + rightValue);
                    } else if(opt == '-') {
                        result.add(leftValue - rightValue);
                    } else if(opt == '*') {
                        result.add(leftValue * rightValue);
                    }
                }
            }
        }
        return result;
    }*/
    /*// 解法二：动态规划（我踏马一遍就写出来了，没有任何bug，牛不牛比）
    public List<Integer> diffWaysToCompute(String expression) {
        List<Integer> numList = new ArrayList<>();
        List<Character> optList = new ArrayList<>();
        int num = 0;
        for(int i=0; i<expression.length(); ++i) {
            char c = expression.charAt(i);
            if(!Character.isDigit(c)) {
                optList.add(c);
                numList.add(num);
                num = 0;
                continue;
            }
            num = num * 10 + (c - '0');
            if(i == expression.length()-1) {
                numList.add(num);
            }
        }
        int n = numList.size();
        List<Integer>[][] dp = new List[n][n];
        // 初始化边界条件
        for(int i=0; i<n; ++i) {
            dp[i][i] = new ArrayList<>();
            dp[i][i].add(numList.get(i));
        }
        // 多个数字组合
        for(int len=1; len<n; ++len) {
            for(int left=0; left<n-len; ++left) {
                int right = left + len;
                dp[left][right] = new ArrayList<>();
                for(int i=left; i<right; ++i) {
                    char opt = optList.get(i);
                    List<Integer> leftResult = dp[left][i];
                    List<Integer> rightResult = dp[i+1][right];
                    for (Integer leftValue : leftResult) {
                        for (Integer rightValue : rightResult) {
                            if(opt == '+') dp[left][right].add(leftValue + rightValue);
                            else if(opt == '-') dp[left][right].add(leftValue - rightValue);
                            else if(opt == '*') dp[left][right].add(leftValue * rightValue);
                        }
                    }
                }
            }
        }
        return dp[0][n-1];
    }*/
    // 解法一改写：直接使用类似于C++的stoi函数，不用手动从String中生成数字
    /*public List<Integer> diffWaysToCompute(String expression) {
        List<Integer> result = new ArrayList<>();
        for(int i=0; i<expression.length(); ++i) {
            if(Character.isDigit(expression.charAt(i))) {
                continue;
            }
            char opt = expression.charAt(i);
            List<Integer> leftResult = diffWaysToCompute(expression.substring(0, i));
            List<Integer> rightResult = diffWaysToCompute(expression.substring(i+1, expression.length()));
            for (Integer leftValue : leftResult) {
                for (Integer rightValue : rightResult) {
                    if(opt == '+') result.add(leftValue + rightValue);
                    else if(opt == '-') result.add(leftValue - rightValue);
                    else result.add(leftValue * rightValue);
                }
            }
        }
        if(result.size() < 1) {
            result.add(Integer.valueOf(expression));
        }
        return result;
    }*/

    /**
     * 二刷
     * 解法：动态规划+分治，也可以说成是带map的分治法，说法不同，但本质是一样的
     */
    public List<Integer> diffWaysToCompute(String expression) {
        List<Integer> nums = new ArrayList<>();
        List<Character> opts = new ArrayList<>();
        handleExpression(nums, opts, expression);
        int n = nums.size();
        List<Integer>[][] dp = new List[n][n];
        for(int i=0; i<n; ++i) {
            dp[i][i] = new ArrayList<>(1);
            dp[i][i].add(nums.get(i));
        }
        for(int step = 1; step < n; ++step) {
            for(int i=0; i+step < dp.length; ++i) {
                int j = i + step;
                dp[i][j] = new ArrayList<>();
                for(int k=i; k<j; ++k) {
                    List<Integer> left = dp[i][k];
                    List<Integer> right = dp[k+1][j];
                    merge(left, right, opts.get(k), dp[i][j]);
                }
            }
        }
        return dp[0][n-1];
    }

    private void merge(List<Integer> left, List<Integer> right, char opt, List<Integer> result) {
        for (int i : left) {
            for (int j : right)
                result.add(calculate(i, j, opt));
        }
    }

    private void handleExpression(List<Integer> nums, List<Character> opts, String expression) {
        int num = 0, i=0;
        while (i < expression.length()) {
            char c = expression.charAt(i++);
            if (Character.isDigit(c)) {
                num = num * 10 + c - '0';
            } else {
                nums.add(num);
                num = 0;
                opts.add(c);
            }
        }
        nums.add(num);
    }

    private int calculate(int a, int b, char opt) {
        if (opt == '*') return a * b;
        if (opt == '+') return a + b;
        else return a - b;
    }
}
